package com.kaiwukj.android.ufamily.mvp.ui.page.service.fee;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.PayFeeCancelRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.PropertyFeeResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */

public class PayFeePresenter extends BasePresenter<ServiceContract.Model, ServiceContract.PropertyView> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public PayFeePresenter(ServiceContract.Model model, ServiceContract.PropertyView rootView) {
        super(model, rootView);
    }

    /**
     * 获取物业缴费列表
     */
    public void requestPayFeeList() {
        mModel.fetchPropertyFee(1)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<List<PropertyFeeResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<PropertyFeeResult> result) {
                        mRootView.onPropertyResult(result);
                    }
                });
    }

    /**
     * 获取物业缴费列表
     */
    public void requestPayFeeList(int type, String payTime) {

        JSONObject jsonObject = new JSONObject();

        try {
            if (type > 0) {
                jsonObject.put("type", type);
            }
            if (!StringUtils.isEmpty(payTime)) {
                jsonObject.put("payTime", payTime);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /*mModel.requestPayFeeLists(jsonObject.toString())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .compose(RxUtils.handleResult())
                .subscribe(new ApiCallBack<ListResp<PayFeeResult>>(mErrorHandler) {
                    @Override
                    public void onSuccess(ListResp<PayFeeResult> resp) {
                        mRootView.showPayFeeList(resp.getList());
                    }
                });*/

        /*
        *
        * new ErrorHandleSubscriber<PayFeeListResult>(mErrorHandler) {
                    @Override
                    public void onNext(PayFeeListResult result) {
                        if (result.getCode().equals(Api.RequestSuccess)) {
                            mRootView.getPayFeeList(result.getResult().getList());
                        } else {
                            mRootView.showMessage(result.getDesc());
                        }
                    }
                }
        * */
    }


    /**
     * 微信支付
     */
    public void payByWechat(int refId, Activity activity) {
        mModel.payfeeProperty(2, refId, 1)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<Map<String, Object>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull Map<String, Object> result) {
                        mRootView.onWXPayResult(result);
                    }
                });
    }

    /**
     * 支付宝支付
     */
    public void payByAli(int refId, Activity activity) {
        mModel.payfeeProperty(1, refId, 1)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Map<String, Object>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull Map<String, Object> result) {
                        mRootView.onAliPayResult(result);
                    }

                });
    }

    /**
     * 取消订单
     */
    public void cancelOrder(PayFeeCancelRequest request) {
      /*  mModel.cancelOrder(request)
                .subscribeOn(Schedulers.io())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.handleCode())
                .subscribe(new ApiCallBack<Integer>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("已取消");
                    }

                });*/
    }

}
